home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / dev / gcc / ixemul_src.lha / ixemul-41.0 / string / strftime.c < prev    next >
C/C++ Source or Header  |  1995-05-17  |  6KB  |  280 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)strftime.c    5.8 (Berkeley) 6/1/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. /*
  25.  * NOTE: I used the older version of this file, since the 4.3bsd-net2
  26.  *     version uses mktime() from ctime.c, and ctime.c is badly suited
  27.  *     for inclusion in a shared library (too much static stuff...)
  28.  */
  29.  
  30.  
  31. #define KERNEL
  32. #include "ixemul.h"
  33.  
  34. #include <sys/types.h>
  35. #include <sys/time.h>
  36. #include <time.h>
  37. #include <tzfile.h>
  38. #include <string.h>
  39.  
  40. static char *afmt[] = {
  41.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  42. };
  43. static char *Afmt[] = {
  44.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  45.     "Saturday",
  46. };
  47. static char *bfmt[] = {
  48.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  49.     "Oct", "Nov", "Dec",
  50. };
  51. static char *Bfmt[] = {
  52.     "January", "February", "March", "April", "May", "June", "July",
  53.     "August", "September", "October", "November", "December",
  54. };
  55.  
  56. static size_t gsize;
  57. static char *pt;
  58. static size_t _fmt(const char *, const struct tm *);
  59. static int _conv (int, int, char);
  60. static int _add (char *);
  61.  
  62. size_t
  63. strftime(s, maxsize, format, t)
  64.     char *s;
  65.     const char *format;
  66.     size_t maxsize;
  67.     const struct tm *t;
  68. {
  69.     size_t res;
  70.  
  71.     ix_lock_base ();
  72.  
  73.     pt = s;
  74.     if ((gsize = maxsize) < 1)
  75.       {
  76.         res = 0;
  77.         goto unlock;
  78.       }
  79.     if (_fmt(format, t)) {
  80.         *pt = '\0';
  81.         res = maxsize - gsize;
  82.         goto unlock;
  83.     }
  84.     res = 0;
  85.  
  86. unlock:
  87.     ix_unlock_base ();
  88.     return res;
  89. }
  90.  
  91. static size_t
  92. _fmt(format, t)
  93.     register const char *format;
  94.     const struct tm *t;
  95. {
  96.     for (; *format; ++format) {
  97.         if (*format == '%')
  98.             switch(*++format) {
  99.             case '\0':
  100.                 --format;
  101.                 break;
  102.             case 'A':
  103.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  104.                     return(0);
  105.                 if (!_add(Afmt[t->tm_wday]))
  106.                     return(0);
  107.                 continue;
  108.             case 'a':
  109.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  110.                     return(0);
  111.                 if (!_add(afmt[t->tm_wday]))
  112.                     return(0);
  113.                 continue;
  114.             case 'B':
  115.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  116.                     return(0);
  117.                 if (!_add(Bfmt[t->tm_mon]))
  118.                     return(0);
  119.                 continue;
  120.             case 'b':
  121.             case 'h':
  122.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  123.                     return(0);
  124.                 if (!_add(bfmt[t->tm_mon]))
  125.                     return(0);
  126.                 continue;
  127.             case 'C':
  128.                 if (!_fmt("%a %b %e %H:%M:%S %Y", t))
  129.                     return(0);
  130.                 continue;
  131.             case 'c':
  132.                 if (!_fmt("%m/%d/%y %H:%M:%S", t))
  133.                     return(0);
  134.                 continue;
  135.             case 'e':
  136.                 if (!_conv(t->tm_mday, 2, ' '))
  137.                     return(0);
  138.                 continue;
  139.             case 'D':
  140.                 if (!_fmt("%m/%d/%y", t))
  141.                     return(0);
  142.                 continue;
  143.             case 'd':
  144.                 if (!_conv(t->tm_mday, 2, '0'))
  145.                     return(0);
  146.                 continue;
  147.             case 'H':
  148.                 if (!_conv(t->tm_hour, 2, '0'))
  149.                     return(0);
  150.                 continue;
  151.             case 'I':
  152.                 if (!_conv(t->tm_hour % 12 ?
  153.                     t->tm_hour % 12 : 12, 2, '0'))
  154.                     return(0);
  155.                 continue;
  156.             case 'j':
  157.                 if (!_conv(t->tm_yday + 1, 3, '0'))
  158.                     return(0);
  159.                 continue;
  160.             case 'k':
  161.                 if (!_conv(t->tm_hour, 2, ' '))
  162.                     return(0);
  163.                 continue;
  164.             case 'l':
  165.                 if (!_conv(t->tm_hour % 12 ?
  166.                     t->tm_hour % 12 : 12, 2, ' '))
  167.                     return(0);
  168.                 continue;
  169.             case 'M':
  170.                 if (!_conv(t->tm_min, 2, '0'))
  171.                     return(0);
  172.                 continue;
  173.             case 'm':
  174.                 if (!_conv(t->tm_mon + 1, 2, '0'))
  175.                     return(0);
  176.                 continue;
  177.             case 'n':
  178.                 if (!_add("\n"))
  179.                     return(0);
  180.                 continue;
  181.             case 'p':
  182.                 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
  183.                     return(0);
  184.                 continue;
  185.             case 'R':
  186.                 if (!_fmt("%H:%M", t))
  187.                     return(0);
  188.                 continue;
  189.             case 'r':
  190.                 if (!_fmt("%I:%M:%S %p", t))
  191.                     return(0);
  192.                 continue;
  193.             case 'S':
  194.                 if (!_conv(t->tm_sec, 2, '0'))
  195.                     return(0);
  196.                 continue;
  197.             case 'T':
  198.             case 'X':
  199.                 if (!_fmt("%H:%M:%S", t))
  200.                     return(0);
  201.                 continue;
  202.             case 't':
  203.                 if (!_add("\t"))
  204.                     return(0);
  205.                 continue;
  206.             case 'U':
  207.                 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
  208.                     2, '0'))
  209.                     return(0);
  210.                 continue;
  211.             case 'W':
  212.                 if (!_conv((t->tm_yday + 7 -
  213.                     (t->tm_wday ? (t->tm_wday - 1) : 6))
  214.                     / 7, 2, '0'))
  215.                     return(0);
  216.                 continue;
  217.             case 'w':
  218.                 if (!_conv(t->tm_wday, 1, '0'))
  219.                     return(0);
  220.                 continue;
  221.             case 'x':
  222.                 if (!_fmt("%m/%d/%y", t))
  223.                     return(0);
  224.                 continue;
  225.             case 'y':
  226.                 if (!_conv((t->tm_year + TM_YEAR_BASE)
  227.                     % 100, 2, '0'))
  228.                     return(0);
  229.                 continue;
  230.             case 'Y':
  231.                 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
  232.                     return(0);
  233.                 continue;
  234.             case 'Z':
  235.                 if (!t->tm_zone || !_add(t->tm_zone))
  236.                     return(0);
  237.                 continue;
  238.             case '%':
  239.             /*
  240.              * X311J/88-090 (4.12.3.5): if conversion char is
  241.              * undefined, behavior is undefined.  Print out the
  242.              * character itself as printf(3) does.
  243.              */
  244.             default:
  245.                 break;
  246.         }
  247.         if (!gsize--)
  248.             return(0);
  249.         *pt++ = *format;
  250.     }
  251.     return(gsize);
  252. }
  253.  
  254. static
  255. _conv(n, digits, pad)
  256.     int n, digits;
  257.     char pad;
  258. {
  259.     static char buf[10];
  260.     register char *p;
  261.  
  262.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  263.         *p-- = n % 10 + '0';
  264.     while (p > buf && digits-- > 0)
  265.         *p-- = pad;
  266.     return(_add(++p));
  267. }
  268.  
  269. static
  270. _add(str)
  271.     register char *str;
  272. {
  273.     for (;; ++pt, --gsize) {
  274.         if (!gsize)
  275.             return(0);
  276.         if (!(*pt = *str++))
  277.             return(1);
  278.     }
  279. }
  280.